<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>修改页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrap-theme.min.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/css/public.css">
<script type="text/javascript" src="${contextPath}/js/jquery.min.js"></script>
<script type="text/javascript" src="${contextPath}/js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="${contextPath}/js/jquery.fileupload.js"></script>
<script type="text/javascript" src="${contextPath}/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${contextPath}/js/common.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-default">
			<div class="panel-heading">待修改任务信息<span id="sendbackMsg" class="pull-right text-success"></span> </div>
			<div class="panel-body">
				<button type="button" id="downTask" class="btn btn-warning">下载待修改任务</button>
				<button type="button" id="isDone" class="btn btn-success">修改完成</button>
				<div style="float: right;">
					<form action="${contextPath}/security/inpsUpTagAndTextGrid" method="post" name="inpsUpTagAndTextGrid" role="form" class="form-inline" enctype="multipart/form-data">
					<label for="TAG" class="control-label">选择要修改的任务</label> &nbsp;&nbsp;
					<input type="file" style="width: 75px; height: 20px; display: inline-block;" name="file" id="fileupload" autocomplete="off" multiple/>&nbsp;&nbsp;
					<span class="help-block" style="display: inline-block;" id="uploadHelp">已选择0个文件</span>&nbsp;&nbsp;
					<button id="uploadBtn" type="button" class="btn btn-primary">上传文件</button>
					</form>
				</div>
				<div style="margin-top:10px;">
					<span class="text-success">列表中黄色背景为被抽检任务&nbsp;&nbsp;红色字体为不合格任务</span>
				</div>
			</div>
			<table class="table table-striped table-bordered">
				<thead>
					<tr> 
						<th colspan="6">退回原因</th>
					</tr>
					<tr> 
						<th colspan="6" style="text-align: left;">
							<span id="sendBackReason" style="font-weight: normal;margin-left: 20px;"></span>
						</th>
					</tr>
					<tr> 
						<th colspan="6">任务列表</th>
					</tr>
					<tr>
						<th width='6%'>序号</th>
						<th width='10%'>工作者</th>
						<th width='20%'>任务名称</th>
						<th width='18%'>标注时间(秒)</th>
						<th width='20%'>退回时间</th>
					</tr>
				</thead>
				<tbody></tbody>
			</table>
		</div>
	</div>
	<script type="text/javascript">
		var sendbackUUID = '${sendbackUUID}';
		var inspectorId = '${inspectorId}';
		var inspectorRecordId = '${inspectorRecordId}';
		var taskList = "";
		$(document).ready(function(){
			loadTaskList();
			
			/*--------------------------------------下载待修改任务-------------------------------------------------------*/
			$("#downTask").click(function(){
				if(taskList.size == 0){
					alert("暂无待修改任务！");
				}else{
					$.ajax({
						type:'POST',
						data:{"list":taskList,"sendbackUUID":sendbackUUID},
						url:'${contextPath}/security/downloadSendbackTasks',
						dataType:'json',
						success:function(data){
							if(data.wrongPath != ""){
								window.open("${contextPath}"+data.wrongPath);
							}
						}
					});
				}
			});
			/*--------------------------------------修改任务-------------------------------------------------------*/
			$("#isDone").click(function(){//成功
				var conWin = confirm("确定修改完毕吗？");
				if(conWin){
					modifyCompleted();
				}
			});
			
			modifyCompleted = function(){
				$.ajax({
					type:'POST',
					data:{"sendbackUUID":sendbackUUID,"inspectorId":inspectorId},
					url:'${contextPath}/security/modifyCompleted',
					dataType:'json',
					traditional:true,
					success:function(data){
						if($(".alert").length>0)$(".alert").remove();
						var $alertMsg = $("<div class='alert alert-dismissable'>"+data.message+"</div>");
						$alertMsg.append("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>");
						if(data.replay == "1"){
							$alertMsg.addClass("alert-success");
						}else{
							$alertMsg.addClass("alert-danger");
						}
						$(".panel-body").prepend($alertMsg);
						loadTaskList();
					}
				});
			};
			
		});
		/*--------------------------------------加载列表-------------------------------------------------------*/
		loadTaskList = function(){
			$.ajax({
				type:'POST',
				data:{"sendbackUUID":sendbackUUID,"inspectorId":inspectorId,"inspectorRecordId":inspectorRecordId},
				url:'${contextPath}/security/sendbackInspectorList',
				dataType:'json',
				success:function(data){
					$("tbody").empty();
					if(data.list == ""){
						$("#sendBackReason").html("");
						$("#downTask").attr("disabled","disabled");
						$("#isDone").attr("disabled","disabled");
						$("tbody").empty();
						$("tbody").append("<tr class='text-danger'><td colspan='6'>无内容</td></tr>");
					}else{
						$("#sendBackReason").html(data.sendbackReason);
						$.each(data.list,function(i,item){
							if(i == 0){
								taskList = item.taskId;
							}else{
								taskList +="_"+item.taskId;
							}
							
							$("tbody").append(
								"<tr id='tr_"+item.taskId+"' class='inspected_"+item.isInspected+" unqualified_"+item.isUnqualified+"'>"+
									"<td>"+(i+1)+"</td>"+
									"<td>"+item.realName+"</td>"+
									"<td>"+item.taskName+"</td>"+
									"<td>"+item.taskMarkTime+"</td>"+
									"<td>"+item.sendbackTime+"<input class='unqualifiedCheckbox' type='hidden' value='"+item.taskId+"'/></td>"+
								"</tr>"
							);
						});
						$(".inspected_true").each(function(){
							$(this).children().css("background-color","yellow");
						});
						$(".unqualified_true").each(function(){
							$(this).children().css("color","red").css("font-weight","bold");
						});
						var msg = "当前批次任务被抽检"+data.inspectedNum+"个，其中不合格"+data.unqualifiedNum+"个。错误率为"+data.unqualifiedNum/data.inspectedNum*100+"%";
						$("#sendbackMsg").html(msg);
					}
				}
			});
		};
		/*******************************多文件上传**************************************************/
			var modifyTaskIds=[];
			$("#fileupload").click(function(){
				modifyTaskIds=[];
				$(".unqualifiedCheckbox").each(function(i){
					modifyTaskIds.push($(this).val());
				});
				
			});
			$("#fileupload").fileupload({
				singleFileUploads : false,
				formAcceptCharset : 'gb2312',
				typ : "POST",
				dataType : "json",
				url : "${contextPath}/security/inpsUpTagAndTextGrid",
				add : function(e, data) {
					var $this = $(this);
					data.autoUpload = false;
					var fileNum = data.files.length;
					//-------------验证开始
					var reg = /(\.|\/)(textgrid)$/i;
					for(var i = 0; i<fileNum;i++){
						if(!(reg.test(data.files[i].name))){//文件格式验证
							alert("选择文件含有格式不正确文件,请重新选择!");
							return false;
						}
					}
					//-------------验证结束
					$("#uploadHelp").text("已选择" + fileNum+ "个文件");
					$("#uploadBtn").click(function() {
						if (data.files.length == 0) {
							$("#uploadHelp").text("请选择上传的文件");
							return false;
						};
						data.submit();
						$(this).attr("disabled","disabled").text("上传中。。");
	
					});
				},
				done : function(e, result) {
					if(result.result.msg=="1"){
						alert("文件修改成功!修改了"+result.result.returnList.length+"个文件! 修改过的文件将被标注红色背景!");
						$("#uploadBtn").removeAttr("disabled").text("上传文件");
						$("#uploadBtn").unbind("click");
						$("#uploadHelp").text("已选择0个文件");
					}else if(result.result.msg=="2"){
						alert("文件修改出错,请联系系统维护人员!");
						$("#uploadBtn").text("上传文件");
					}else if(result.result.msg=="3"){
						alert("没有文件被更改,如有问题请联系系统维护人员!");
						$("#uploadBtn").removeAttr("disabled").text("上传文件");
						$("#uploadBtn").unbind("click");
						$("#uploadHelp").text("已选择0个文件");
					}
					
					for(var i = 0; i<result.result.returnList.length;i++){
						$("#tr_"+result.result.returnList[i]).find("td").each(function(){
							$(this).css("background-color","red");
						});
					}
					
				}
			});
			 //文件上传前触发事件
		    $("#fileupload").bind("fileuploadsubmit", function (e, data) {
		        data.formData = { "taskIds": modifyTaskIds };  
		    });
	</script>
</body>
</html>
